﻿layui.config({
    base: '/Plugin/',
}).use(['element', 'jquery', 'layer', 'table', 'form', 'md5'], function () {
    var element = layui.element,
        layer = layui.layer,
        table = layui.table,
        form = layui.form,
        md5 = layui.md5,
        $ = layui.jquery,
        jhy = layui.data("jhy"),
        fromurl = layui.data("url");

    //判断是否有本地存储的用户信息
    if (jhy.user_id != undefined && jhy.user_account != undefined && jhy.user_password != undefined && jhy.user_name != undefined && jhy.user_role != undefined) {
        //ajax 验证账号和密码
        jhy_user_login(jhy.user_account, jhy.password);
    } else {
        //没有这些信息则清空表。
        layui.data("jhy", null);
    }


    //登录按钮操作 
    form.on('submit(login)', function (data) {
        var data = data.field;
        var account = $.trim(data.account);
        var password = $.trim(data.password);
        //ajax提交数据验证数据
        jhy_user_login(account, md5(password));
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });


    function jhy_user_login(user_account, user_password) {
        $.ajax({
            type: "post",
            url: "/Ashx/admin.ashx",
            data: { functionname: "jhy_user_login", user_account: user_account, user_password: user_password },
            dataType: 'json',
            success: function (msg) {
                if (msg.error == '0') {
                    layer.msg("用户名或密码错误", { icon: 5 });
                } else {
                    layui.data('jhy', {
                        key: 'user_id',
                        value: msg[0].user_id
                    });
                    layui.data('jhy', {
                        key: 'user_account',
                        value: msg[0].user_account
                    });
                    layui.data('jhy', {
                        key: 'user_name',
                        value: msg[0].user_name
                    });
                    layui.data('jhy', {
                        key: 'user_password',
                        value: msg[0].user_password
                    });
                    layui.data('jhy', {
                        key: 'user_role',
                        value: msg[0].user_role
                    });


                    //如果存在来向跳转页面则跳转到来向页面，不存则进入后台界面
                    if (fromurl.url != undefined) {
                        window.location.href = fromurl.url;
                    } else {
                        window.location.href = "/admin/iframe.html";
                    }

                }
            }
        });
    }


    //深度刷新
    $(".refresh").click(function () {
        window.location.reload(true);
    });



});  
